{extend name="public/header" /}

{block name="title"}{//$purviewTreeArr[$currentPurviewId]['purview_name']}{/block}
{block name="css"}
<link rel="stylesheet" href="/static/admin/css/font.css">
<link rel="stylesheet" href="/static/admin/lib/layui/css/eleTree.css" media="all">
<link rel="stylesheet" href="/static/admin/lib/ztree/css/metroStyle/metroStyle.css" type="text/css">
<style>
    .ztree * {
        font-size: 14px;
    }
</style>
{/block}
{block name="js"}
<script type="text/javascript" src="/static/admin/js/xadmin.js"></script>
<script type="text/javascript" src="/static/admin/lib/ztree/js/jquery.ztree.core.js"></script>
<script type="text/javascript" src="/static/admin/lib/ztree/js/jquery.ztree.excheck.js"></script>
{/block}
{block name="body"}
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <ul id="purviewTree" class="ztree"></ul>
                        </div>
                        <div class="layui-form-item">
                            <button class="layui-btn" lay-filter="add" lay-submit="">
                                分配管理权限
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    var zTreeObj;
    var zTree = $.fn.zTree;
    // zTree 的参数配置，深入使用请参考 API 文档（setting 配置详解）
    var setting = {
        view: {
            showIcon: false
        },
        data: {
            simpleData: {
                enable: true
            }
        },
        check: {
            enable: true,
            chkStyle: "checkbox",
            chkboxType: {"Y": "ps", "N": "s"}
        },
    };

    var data = '{:json_encode($arrList)}'
    data = eval('(' + data + ')');

    $(document).ready(function () {
        zTreeObj = $.fn.zTree.init($("#purviewTree"), setting, data);
        zTreeObj.expandAll(true)
    });

    layui.config({ base: "/static/admin/lib/layui/lay/mymodules/"}).use(['jquery', 'form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer;

        //监听提交
        form.on('submit(add)', function (obj) {
            var treeObj = zTree.getZTreeObj("purviewTree");
            var nodes = zTreeObj.transformToArray(treeObj.getNodes());

            var purviewIds = [];
            for (i = 0; i < nodes.length; i++) {
                if (nodes[i].checked) {
                    purviewIds.push(nodes[i].id)
                }
            }
            $.ajax({
                type: "post",
                url: obj.form.action,
                data: {'purview_ids': purviewIds},
                dataType: "json",
                success: function (res) {
                    if (res.code != 0) {
                        layer.msg(res.msg, {
                            icon: 2,
                            time: 2000//2秒关闭（如果不配置，默认是3秒）
                        });
                    } else {
                        layer.msg(res.msg, {
                            icon: 1,
                            time: 1000 //2秒关闭（如果不配置，默认是3秒）
                        }, function () {
                            xadmin.close();
                        })
                    }
                }
            })
            return false;
        })
    })

</script>
{/block}
</body>
